package com.wsc.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wsc.goods.domain.GoodsTypePo;
import com.wsc.goods.mapper.GoodsTypeMapper;
import com.wsc.goods.service.GoodsTypeService;
import com.wsc.interfaces.RedisInterface;
import com.wsc.interfaces.TemplateInterface;
import com.wsc.tools.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GoodsTypeServiceImpl extends ServiceImpl<GoodsTypeMapper, GoodsTypePo> implements GoodsTypeService {


    @Autowired
    GoodsTypeMapper goodsTypeMapper;

    @Autowired
    RedisInterface redisInterface;

    @Autowired
    TemplateInterface templateInterface;

    ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 添加节点
     * @param po 节点实体
     * @return 是否成功
     */
    public Integer addNode(GoodsTypePo po) {
        int i = goodsTypeMapper.addNode(po);
        return i;
    }

    /**
     * 添加path
     * @param po 节点实体
     * @return 是否成功
     */
    public Integer modNode(GoodsTypePo po) {
        return null;
    }

    /**
     * 加载树
     * @return 树
     */
    public List<GoodsTypePo> getTree(GoodsTypePo po) {
        List<GoodsTypePo> trees = new ArrayList<GoodsTypePo>();
        try {
            redisInterface.setValue("tree",objectMapper.writeValueAsString(makeTree(po)));
        }catch (Exception e){
            e.printStackTrace();
        }
        try {
            String rValue = redisInterface.getValue("tree");
            trees = objectMapper.readValue(rValue,List.class);
        }catch (Exception e){
            e.printStackTrace();
        }
        return trees;
    }

    /**
     * 创建模板
     * @return 是否成功
     */
    public AjaxResult createTemplate() {
        final String ClASSPATH =this.getClass().getResource("/").getPath();
       // ResourceUtils.getFile("GoodsTypeServiceImpl");
        GoodsTypePo po = new GoodsTypePo();
        //生成product.type.vm.html
        String path = ClASSPATH+"template/home/product.type.vm";
        String toPath = ClASSPATH+"template/product.type.vm.html";
        //String toPath = "E:/product.type.vm.html";
        po.setName("");
        List<GoodsTypePo> tree = getTree(po);
        templateInterface.writeTemplate(path,toPath,"model",tree);
        //生成显示页面
        path = GoodsTypeServiceImpl.class.getResource("/template/home.vm").getPath();
        toPath = "E:/学习资料/商城前端框架/ecommerce/home.html";
        Map<String,String> map = new HashMap<String, String>();
        map.put("staticRoot",GoodsTypeServiceImpl.class.getResource("/").getPath());
        return templateInterface.writeTemplate(path,toPath,"model",map);
    }

    /**
     * 加载树
     * @param po 实体
     * @return 树
     */
    private List<GoodsTypePo> makeTree(GoodsTypePo po) {
        List<GoodsTypePo> tree = list(new QueryWrapper<GoodsTypePo>().like("name",po.getName()));
        List<GoodsTypePo> pTree = new ArrayList<GoodsTypePo>();
        Map<Long,GoodsTypePo> map = new HashMap<Long, GoodsTypePo>();
        for (GoodsTypePo tr : tree){
            if(tr.getPid() == 0) pTree.add(tr);
            map.put(tr.getId(),tr);
        }
        for(GoodsTypePo tr :tree){
            GoodsTypePo parent = map.get(tr.getPid());
            if(parent == null) continue;
            if(parent.getChildren() == null) parent.setChildren(new ArrayList<GoodsTypePo>());
            parent.getChildren().add(tr);
        }

        return pTree;

    }


}
